﻿using Microsoft.AspNetCore.Mvc;
using WebAPI.Dao;
using WebAPI.Entities;
using WebAPI.Services;

namespace WebAPI.Controllers;

[ApiController]
[Route("api/[controller]/[Action]")]
public class CommentController : ControllerBase
{
    private CommentService _commentService = new CommentService();
    
    [HttpPost(Name = "PublishComment")]
    public string PublishComment(Comment comment)
    {
        bool flag = _commentService.AddComment(comment);
        if (!flag)
        {
            return ResultData.Fail("发布评论失败，请稍后重试");
        }
        return ResultData.Ok("发布评论失败");
    }
    
    [HttpGet(Name = "GetComments")]
    public string GetComments(string article_id)
    {
        List<Comment> comments = _commentService.GetAllComments(article_id);
        if (comments.Count == 0)
        {
            return ResultData.Fail("获取评论失败，请稍后重试");
        }
        return ResultData.Ok(comments);
    }
    
    [HttpPut(Name = "DeleteComment")]
    public string DeleteComment(Comment comment)
    {
        bool flag = _commentService.DeleteComment(comment);
        if (!flag)
        {
            return ResultData.Fail("删除评论失败，请稍后重试");
        }
        return ResultData.Ok("删除成功");
    }
    
    [HttpPut(Name = "UpdateComment")]
    public string UpdateComment(Comment comment)
    {
        bool flag = _commentService.UpdateComment(comment);
        if (!flag)
        {
            return ResultData.Fail("删除文章失败，请稍后重试");
        }
        return ResultData.Ok("更新成功");
    }
}